<template>
  <div class="articleAdd-container">
    <!-- 卡片 -->
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <!-- 面包屑导航组件 -->
        <bread-crumb>发布文章</bread-crumb>
      </div>
      <!-- 表单区 -->
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" >
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title"></el-input>
        </el-form-item>

        <el-form-item label="内容" prop="content">
          <!-- 富文本编辑器 -->
          <quill-editor v-model="form.content" :options="editorOption">
          </quill-editor>
        </el-form-item>

        <el-form-item label="封面">
          <!-- 封面类型单选框 这里的v-model用于初始化 -->
          <el-radio-group @change="hChangeCover" v-model="form.coverType">
            <el-radio :label="1">单图</el-radio>
            <el-radio :label="3">三图</el-radio>
            <el-radio :label="0">无图</el-radio>
            <el-radio :label="-1">自动</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- 封面上传区 -->
         <!-- 单图/三图 才显示 -->
        <el-form-item v-if="form.coverType>0">
          <!-- layout布局 - 分栏间隔 -->
          <el-row :gutter="20">
            <!-- v-for 循环数字表示循环的次数 -->
            <el-col :span="6" v-for="index in form.coverType" :key="index">
               <!-- 封面组件 --> {{index}}
              <cover v-model='form.coverImage[index]'/>
            </el-col>
          </el-row>
        </el-form-item>

        <!-- 频道组件 -->
        <channel v-model="form.channelId"></channel>

        <!-- 按钮 -->
        <el-form-item>
          <!-- 根据接口文档: 传参为true 存为草稿 -->
          <el-button type="primary" @click="hAdd(false)">发布</el-button>
          <el-button @click="hAdd(true)">存入草稿</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
// 引入富文本编辑器vue-quill-editor (局部组件)
// 样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
// 组件
import { quillEditor } from 'vue-quill-editor'
import cover from '../../components/cover.vue'

export default {
  name: 'articleAdd',
  components: {
    quillEditor,
    cover
  },
  data () {
    return {
      form: {
        channelId: '', // 频道id
        title: '哈哈哈哈哈哈',
        content: '丸子小丸子',
        coverType: 0, // 封面选择 ,0表示无图
        coverImage: [] // 封面图片
      },
      // 表单验证
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' },
          { min: 5, max: 20, message: '长度在 5 到 20 个字符', trigger: 'change' }
        ],
        content: [
          { required: true, message: '请输入文章内容', trigger: 'blur' },
          { min: 20, max: 66666, message: '内容至少20个字符 哦O.O', trigger: 'change' }
        ],
        channelId: [
          { required: true, message: '请选择频道', trigger: 'blur' }
        ]
      },
      // 富文本配置对象
      editorOption: {
        // 占位配置
        placeholder: '',
        modules: {
          // 配置工具栏
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            ['blockquote', 'code-block'],
            [{ header: 1 }, { header: 2 }],
            [{ list: 'ordered' }, { list: 'bullet' }],
            [{ indent: '-1' }, { indent: '+1' }],
            ['image']
          ]
        }
      }
    }
  },
  methods: {
    buildParam () {
      // 解构赋值
      const { channelId, title, content, coverType, coverImage } = this.form
      const data = {
        // 添加参数-频道id
        channel_id: channelId,
        title: title,
        content: content,
        // 封面参数
        cover: {
          type: coverType,
          images: coverImage
        }
      }
      return data
    },
    // 发送 添加文章请求
    async doAdd (judge) {
      // 获取参数
      const data = this.buildParam()
      try {
        const res = await this.$axios({
          method: 'POST',
          url: '/mp/v1_0/articles?draft=' + judge,
          data
        })
        console.log(res)
        const goodMes = judge ? '添加草稿成功' : '发布文章成功'
        this.$message.success(goodMes)
      } catch (err) {
        console.log(err)
        this.$message.error('添加文章失败')
      }
    },
    // 用户发布文章
    hAdd (judge) {
      // 表单校验
      // validate是ele-ui form的方法, validate(回调函数(两个参数){})-参数:校验成功和未通过校验的字段
      this.$refs.form.validate((valid) => {
        // 验证通过
        if (valid) {
          this.doAdd(judge)
        }
      })
    },
    // 更改封面选择类型
    hChangeCover () {
      // 单图/三图切换时, 存在数据缓存问题
      if (this.form.coverType === 1) {
        // 三图变单图时只保存第一个图片数据
        // 数组.length = 1 , 只保存arr[0]的值
        this.form.coverImage.length = 1
      }
    }
  }
}
</script>

<style scoped lang='less'>
/* 富文本编辑器样式 */
// 在父组件中去设置子组件的样式，加上/deep/ 实现穿透功能
.articleAdd-container /deep/ .ql-editor{
  height: 300px;
}
.articleAdd-container /deep/ .ql-toolbar.ql-snow{
  padding: 0 8px;
}
</style>
